package com.example.bloodbankapplication.service;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class BloodBankService extends IntentService{

	public BloodBankService() {
		super("");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		
		try {
			URL url = new URL(intent.getStringExtra("URL"));
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			
			if(intent.getStringExtra("Screen").equalsIgnoreCase("LoginFragment")){
				urlConnection.setRequestMethod("POST");
				urlConnection.setRequestProperty("Content-type","application/json");
			}
			if(intent.getStringExtra("Screen").equalsIgnoreCase("ListOfDonors")){
				urlConnection.setRequestMethod("GET");
				urlConnection.setRequestProperty("Content-type","application/json");
				urlConnection.setRequestProperty("Authorization", "OAuth "+intent.getStringExtra("AccessToken"));
			}
			
			if(intent.getStringExtra("Screen").equalsIgnoreCase("DonorSearchFragment")){
				urlConnection.setRequestMethod("GET");
				urlConnection.setRequestProperty("Content-type","application/json");
				urlConnection.setRequestProperty("Authorization", "OAuth "+intent.getStringExtra("AccessToken"));
			}
			
			
			if(intent.getStringExtra("Screen").equalsIgnoreCase("Resgistration")){
				
				urlConnection.setRequestMethod("POST");
				urlConnection.setRequestProperty("Content-type","application/json");
				urlConnection.setRequestProperty("Authorization", "OAuth "+intent.getStringExtra("AccessToken"));
				
				
				//Body
				JSONObject jsonObject = new JSONObject();
				
				
				
				jsonObject.put("Name",intent.getStringExtra("FN"));
				jsonObject.put("Age__c",intent.getStringExtra("Age"));
				jsonObject.put("BloodGroup__c",intent.getStringExtra("BGroup"));
				jsonObject.put("City__c",intent.getStringExtra("City"));
				jsonObject.put("Email__c",intent.getStringExtra("Email"));
				jsonObject.put("Gender__c",intent.getStringExtra("gender"));
				jsonObject.put("LastName__c",intent.getStringExtra("Ln"));
				jsonObject.put("MiddleName__c",intent.getStringExtra("MN"));
				jsonObject.put("Phone__c",intent.getStringExtra("Phno"));
				jsonObject.put("State__c",intent.getStringExtra("State"));
				jsonObject.put("ZipCode__c",intent.getStringExtra("Zip"));
				
				Log.i("NCS","JsonResponse: "+jsonObject.toString());	
				          
				urlConnection.setUseCaches(false);
				urlConnection.setDoInput(true);
				urlConnection.setDoOutput(true);

			      DataOutputStream wr = new DataOutputStream (urlConnection.getOutputStream ());
			      wr.writeBytes (jsonObject.toString());
			      wr.flush ();
			      wr.close ();

			}
			
			/*if(intent.getStringExtra("Screen").equalsIgnoreCase("ListOfDonor")){
				urlConnection.setRequestMethod("GET");
				urlConnection.setRequestProperty("Content-type","application/json");
			}*/
			
			
			Log.i("NCS","Response code is :"+urlConnection.getResponseCode());
			Log.i("NCS","Response Method  is :"+urlConnection.getRequestMethod());
			Log.i("NCS","Response Message is   :"+urlConnection.getResponseMessage());
			
			
				if(urlConnection.getResponseCode()== 400){
					String failure = null;
					if(intent.getStringExtra("Screen").equalsIgnoreCase("LoginFragment")){
					Intent i = new Intent(intent.getStringExtra("Action"));
					i.putExtra("Key", failure);
					sendBroadcast(i);
				}
			}
			
			
			String response = convert_To_String(urlConnection.getInputStream());
			
			
			
			Intent i = new Intent(intent.getStringExtra("Action"));
			i.putExtra("Key", response);
			i.putExtra("Category", intent.getStringExtra("Type"));
			sendBroadcast(i);
			
			
			
			
		} catch (Exception e) {
			
			e.printStackTrace();
			Log.i("NCS","iam in exception");
		}
		
	}

	private   String convert_To_String(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {

				sb.append(line + "\n");
				Log.i("TPT", "Line is  :" + line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {                 
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}     


	
	/*@Override
	public void onCreate() {
		super.onCreate();
		Log.i("NCS","on Create");
	}*/
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i("NCS","onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		Log.i("NCS","onDestroy");
		super.onDestroy();
	}

}
